<template>
  <div class="p-2">
    <!--<transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
           <el-form-item label="描述" prop="description">
            <el-input v-model="queryParams.description" placeholder="请输入描述" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="指标体系类型" prop="category">
            <el-select v-model="queryParams.category" placeholder="请选择指标体系类型" clearable>
              <el-option v-for="dict in assess_system_category" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="父节点" prop="parentId">
            <el-input v-model="queryParams.parentId" placeholder="请输入父节点" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="节点类型" prop="systemType">
            <el-select v-model="queryParams.systemType" placeholder="请选择节点类型" clearable>
              <el-option v-for="dict in assess_node_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item> 
        </el-form>
      </div>
    </transition>-->

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd()"
              v-hasPermi="['assess/system:systemType:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="Sort" @click="handleToggleExpandAll">展开/折叠</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="false"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="systemTypeList" row-key="id" :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" ref="systemTypeTableRef"
        @row-dblclick="handleRowDblClick">
        <el-table-column label="名称" prop="name" />
        <!-- <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="指标体系类型" align="center" prop="category">
          <template #default="scope">
            <dict-tag :options="assess_system_category" :value="scope.row.category" />
          </template>
        </el-table-column>
        <el-table-column label="父节点" align="center" prop="parentId" />
        <el-table-column label="节点类型" align="center" prop="systemType">
          <template #default="scope">
            <dict-tag :options="assess_node_type" :value="scope.row.systemType" />
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['assess/system:systemType:edit']" />
            </el-tooltip>
            <el-tooltip content="新增" placement="top">
              <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)"
                v-hasPermi="['assess/system:systemType:add']" />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['assess/system:systemType:remove']" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加或修改指标体系列对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="systemTypeFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="父节点" prop="parentId">
          <el-tree-select v-model="form.parentId" :data="systemTypeOptions"
            :props="{ value: 'id', label: 'name', children: 'children' }" value-key="id" placeholder="请选择父节点"
            check-strictly />
        </el-form-item>
        <el-form-item label="节点类型" prop="systemType">
          <el-select v-model="form.systemType" placeholder="请选择节点类型">
            <el-option v-for="dict in assess_node_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指标体系类型" prop="category" v-if="form.systemType == 'assess_node_type_data'">
          <el-select v-model="form.category" placeholder="请选择指标体系类型">
            <el-option v-for="dict in assess_system_category" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SystemType" lang="ts">
import { addSystemType, delSystemType, getSystemType, listSystemType, updateSystemType } from "@/api/assess/system/systemType";
import { SystemTypeForm, SystemTypeQuery, SystemTypeVO } from '@/api/assess/system/systemType/types';


const props = defineProps({
  status: {
    type: Object,
    default() {
      return {}
    }
  }
})

type SystemTypeOption = {
  id: number;
  name: string;
  children?: SystemTypeOption[];
}

const { proxy } = getCurrentInstance() as ComponentInternalInstance;;

const { assess_system_category, assess_node_type } = toRefs<any>(proxy?.useDict('assess_system_category', 'assess_node_type'));

const systemTypeList = ref<SystemTypeVO[]>([]);
const systemTypeOptions = ref<SystemTypeOption[]>([]);
const buttonLoading = ref(false);
const showSearch = ref(true);
const isExpandAll = ref(true);
const loading = ref(false);

const queryFormRef = ref<ElFormInstance>();
const systemTypeFormRef = ref<ElFormInstance>();
const systemTypeTableRef = ref<ElTableInstance>()

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});


const initFormData: SystemTypeForm = {
  id: undefined,
  name: undefined,
  description: undefined,
  category: undefined,
  parentId: undefined,
  systemType: undefined,
}

const data = reactive<PageData<SystemTypeForm, SystemTypeQuery>>({
  form: { ...initFormData },
  queryParams: {
    name: undefined,
    description: undefined,
    category: undefined,
    parentId: undefined,
    systemType: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父节点不能为空", trigger: "blur" }
    ],
    systemType: [
      { required: true, message: "节点类型不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询指标体系列列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSystemType(queryParams.value);
  const data = proxy?.handleTree<SystemTypeVO>(res.data, "id", "parentId");
  if (data) {
    systemTypeList.value = data;
    loading.value = false;
  }
}

/** 查询指标体系列下拉树结构 */
const getTreeselect = async () => {
  const res = await listSystemType();
  systemTypeOptions.value = [];
  const data: SystemTypeOption = { id: 0, name: '顶级节点', children: [] };
  data.children = proxy?.handleTree<SystemTypeOption>(res.data, "id", "parentId");
  systemTypeOptions.value.push(data);
}

// 取消按钮
const cancel = () => {
  reset();
  dialog.visible = false;
}

// 表单重置
const reset = () => {
  form.value = { ...initFormData }
  systemTypeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 新增按钮操作 */
const handleAdd = (row?: SystemTypeVO) => {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  dialog.visible = true;
  dialog.title = "添加指标体系列";
}

/** 展开/折叠操作 */
const handleToggleExpandAll = () => {
  isExpandAll.value = !isExpandAll.value;
  toggleExpandAll(systemTypeList.value, isExpandAll.value)
}

/** 展开/折叠操作 */
const toggleExpandAll = (data: SystemTypeVO[], status: boolean) => {
  data.forEach((item) => {
    systemTypeTableRef.value?.toggleRowExpansion(item, status)
    if (item.children && item.children.length > 0) toggleExpandAll(item.children, status)
  })
}

/** 修改按钮操作 */
const handleUpdate = async (row: SystemTypeVO) => {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  const res = await getSystemType(row.id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改指标体系列";
}

/** 提交按钮 */
const submitForm = () => {
  systemTypeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSystemType(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addSystemType(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row: SystemTypeVO) => {
  await proxy?.$modal.confirm('是否确认删除指标体系列编号为"' + row.id + '"的数据项？');
  loading.value = true;
  await delSystemType(row.id).finally(() => loading.value = false);
  await getList();
  proxy?.$modal.msgSuccess("删除成功");
}

const handleRowDblClick = (row) => {
  props.status.systemTypeData = row;
}

onMounted(() => {
  getList();
});
</script>
